#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

int main()
{

    int fd =  open("test.txt",O_RDWR);

    if (fd < 0 )
    {
        printf("打开文件失败\n");
        perror("open");
        return 1;
    }   
    char buf[17] = {0};

    ssize_t read_len = read(fd,buf,16);
    
    if (read_len < 0)
    {
        printf("读取数据失败\n");
        perror("read");
        
        return 1;
    }

    // ssize_t write_len = write(STDOUT_FILENO,buf,read_len); //把文件信息写到控制台

    // write(fd,"\n",1);      //先在文件里写入一个换行
    ssize_t write_len = write(fd,buf,read_len);//再把读到的文件信息写入文件

    if (write_len < 0)
    {
        printf("写入数据失败\n");
        perror("write");
        return 1;
    }

    int result = close(fd);
    if (result < 0)
    {
       printf("关闭文件失败\n");
       perror("close");
       return 1;
    }

    return 0;
}